//
//  PlayerView.m
//  AVAudioPlayerDemo
//
//  Created by 小草毋正 on 2018/1/4.
//  Copyright © 2018年 C-LOVE. All rights reserved.
//

#import "PlayerView.h"


@interface PlayerView ()

/** 进度条 */
@property (weak, nonatomic) IBOutlet UISlider *progressSilder;
@property (weak, nonatomic) IBOutlet UIButton *preViousButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property (weak, nonatomic) IBOutlet UIButton *playerButton;
@property (weak, nonatomic) IBOutlet UIButton *listButton;

@property (weak, nonatomic) IBOutlet UILabel *curentTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *totleTimeLabel;




@end



@implementation PlayerView


- (void)awakeFromNib {
    [super awakeFromNib];
}


- (void)setTotalSecond:(NSInteger)totalSecond {
    _totalSecond = totalSecond;
    self.totleTimeLabel.text = [self timeShowWithSecond:totalSecond]; 
}

- (void)setCurentSecond:(NSInteger)curentSecond {
    _curentSecond = curentSecond;
    self.curentTimeLabel.text = [self timeShowWithSecond:curentSecond];
    if(self.totalSecond != 0 && curentSecond != 0){
        self.progressSilder.value = curentSecond/(float)self.totalSecond;
    }
    else {
        self.progressSilder.value = 0;
    } 
}

- (void)setPlaying:(BOOL)playing {
    _playing = playing;
    if(playing){
        [self.playerButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    }
    else {
        [self.playerButton setImage:[UIImage imageNamed:@"player"] forState:UIControlStateNormal];
    }
    
}

- (IBAction)slider:(UISlider *)sender {
    NSLog(@"change %f", sender.value);
    if([self.delegate respondsToSelector:@selector(playerToProgress:)]){
        [self.delegate playerToProgress:sender.value];
    }
}

- (IBAction)persiousAction:(id)sender {
    if([self.delegate respondsToSelector:@selector(playerViewPersious)]){
        [self.delegate playerViewPersious];
    }
}
- (IBAction)nextAction:(id)sender {
    if([self.delegate respondsToSelector:@selector(playerViewNext)]){
        [self.delegate playerViewNext];
    }
}
- (IBAction)playingAction:(id)sender {
    if([self.delegate respondsToSelector:@selector(playerViewPlaying)]){
        self.playing = [self.delegate playerViewPlaying];
    }
}



/** 根据秒显示对应的时间 */
- (NSString *)timeShowWithSecond:(NSInteger)second {
    NSInteger min = second / 60;
    NSInteger sec = second % 60;
    NSString *text = [NSString stringWithFormat:@"%ld:%02ld", min, sec];
    return text;
}


@end


